﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApMlmSharedLogic
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.MultipleLocationsManipulation;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp.OLP_Utilities;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CApMlmSharedLogic : IDisposable
{
  private TxOlpRobotFollowMode _followMode;
  private TxManipulator _coneManipulator;
  private ITxLocationOperation _location;
  private bool _showConeManipulator;
  private TxFrame _tmpFrame;
  private TxFrame _tmpProjectionFrame;
  private TxFrame _workingFrame;
  private TxFrame _placementManipulatorFrame;
  private CApMlmSharedLogic.eGunAndRobotOnLocationState _lastGunAndRobotOnLocationState;
  private const int EPSILON = 6;
  private const double TRANSFORMATION_MATRIX_CALCULATION_EPSILON = 1E-05;
  private static readonly Color ACTIVE_BUTTON_BORDER_COLOR = Color.FromArgb(51, 153, (int) byte.MaxValue);
  private static readonly Color ACTIVE_BUTTON_BACKGROUND_COLOR = Color.FromArgb(137, 196, (int) byte.MaxValue);

  public bool ShowConeManipulator
  {
    get => this._showConeManipulator;
    set
    {
      this._showConeManipulator = value;
      this.GraphicsRefresh();
    }
  }

  public ITxLocationOperation Location
  {
    get => this._location;
    set
    {
      this._location = value;
      this.GraphicsRefresh();
    }
  }

  public CApMlmSharedLogic.eGunAndRobotOnLocationState LastGunAndRobotOnLocationState
  {
    get => this._lastGunAndRobotOnLocationState;
    set => this._lastGunAndRobotOnLocationState = value;
  }

  internal static ReferenceAxis GetSelectedAxis(ITxLocationOperation location, string action)
  {
    ReferenceAxis selectedAxis = (ReferenceAxis) 2;
    bool flag1 = true;
    bool flag2 = true;
    bool flag3 = true;
    switch (location)
    {
      case TxWeldLocationOperation _:
        bool flag4 = action == MlmStringTable.MLM_APPLY_ROTATION_AXIS_PERPENDICULAR;
        switch ((int) TxApplication.Options.Weld.Perpendicular)
        {
          case 0:
            flag1 = false;
            if (flag4)
            {
              selectedAxis = (ReferenceAxis) 0;
              break;
            }
            break;
          case 1:
            flag2 = false;
            if (flag4)
            {
              selectedAxis = (ReferenceAxis) 1;
              break;
            }
            break;
          case 2:
            flag3 = false;
            if (flag4)
            {
              selectedAxis = (ReferenceAxis) 2;
              break;
            }
            break;
        }
        bool flag5 = action == MlmStringTable.MLM_APPLY_ROTATION_AXIS_APPROACH;
        switch ((int) TxApplication.Options.Weld.Approach)
        {
          case 0:
          case 3:
            flag1 = false;
            if (flag5)
            {
              selectedAxis = (ReferenceAxis) 0;
              break;
            }
            break;
          case 1:
          case 4:
            flag2 = false;
            if (flag5)
            {
              selectedAxis = (ReferenceAxis) 1;
              break;
            }
            break;
          case 2:
          case 5:
            flag3 = false;
            if (flag5)
            {
              selectedAxis = (ReferenceAxis) 2;
              break;
            }
            break;
        }
        if (action == MlmStringTable.MLM_APPLY_ROTATION_AXIS_THIRD_AXIS)
        {
          if (flag1)
          {
            selectedAxis = (ReferenceAxis) 0;
            break;
          }
          if (flag2)
          {
            selectedAxis = (ReferenceAxis) 1;
            break;
          }
          if (flag3)
          {
            selectedAxis = (ReferenceAxis) 2;
            break;
          }
          break;
        }
        break;
      case TxRoboticSeamLocationOperation _:
        bool flag6 = action == MlmStringTable.MLM_APPLY_ROTATION_AXIS_NORMAL;
        switch ((int) TxApplication.Options.Continuous.Normal)
        {
          case 0:
          case 3:
            flag1 = false;
            if (flag6)
            {
              selectedAxis = (ReferenceAxis) 0;
              break;
            }
            break;
          case 1:
          case 4:
            flag2 = false;
            if (flag6)
            {
              selectedAxis = (ReferenceAxis) 1;
              break;
            }
            break;
          case 2:
          case 5:
            flag3 = false;
            if (flag6)
            {
              selectedAxis = (ReferenceAxis) 2;
              break;
            }
            break;
        }
        bool flag7 = action == MlmStringTable.MLM_APPLY_ROTATION_AXIS_MOVEMENT;
        switch ((int) TxApplication.Options.Continuous.MovementVector)
        {
          case 0:
          case 3:
            flag1 = false;
            if (flag7)
            {
              selectedAxis = (ReferenceAxis) 0;
              break;
            }
            break;
          case 1:
          case 4:
            flag2 = false;
            if (flag7)
            {
              selectedAxis = (ReferenceAxis) 1;
              break;
            }
            break;
          case 2:
          case 5:
            flag3 = false;
            if (flag7)
            {
              selectedAxis = (ReferenceAxis) 2;
              break;
            }
            break;
        }
        if (action == MlmStringTable.MLM_APPLY_ROTATION_AXIS_THIRD_AXIS)
        {
          if (flag1)
          {
            selectedAxis = (ReferenceAxis) 0;
            break;
          }
          if (flag2)
          {
            selectedAxis = (ReferenceAxis) 1;
            break;
          }
          if (flag3)
          {
            selectedAxis = (ReferenceAxis) 2;
            break;
          }
          break;
        }
        break;
    }
    return selectedAxis;
  }

  internal static double GetBoundedAngle(double angle)
  {
    double num = Math.Round(Math.PI, 6);
    angle = Math.Round(angle, 6);
    double boundedAngle = angle % (2.0 * num);
    if (angle > num)
      boundedAngle = angle - 2.0 * num;
    else if (angle < -1.0 * num)
      boundedAngle = angle + 2.0 * num;
    return boundedAngle;
  }

  internal void ResetAbsoluteLocation(List<ITxRoboticLocationOperation> locations)
  {
    foreach (ITxRoboticLocationOperation location in locations)
      this.ResetAbsoluteLocation(location, false);
    this.GraphicsRefresh();
  }

  internal void ResetAbsoluteLocation(
    ITxRoboticLocationOperation location,
    bool shouldRefreshGraphics = true)
  {
    switch (location)
    {
      case TxWeldLocationOperation _:
        (location as TxWeldLocationOperation).AbsoluteLocation = (location as TxWeldLocationOperation).ProjectedLocation;
        break;
      case TxRoboticSeamLocationOperation _:
        (location as TxRoboticSeamLocationOperation).AbsoluteLocation = (location as TxRoboticSeamLocationOperation).ProjectedLocation;
        break;
      default:
        shouldRefreshGraphics = false;
        break;
    }
    if (!shouldRefreshGraphics)
      return;
    this.GraphicsRefresh();
  }

  internal void SnapToMaxAloowed(List<ITxRoboticLocationOperation> locations)
  {
    foreach (ITxRoboticLocationOperation location in locations)
      this.SnapToMaxAloowed(location, false);
    this.GraphicsRefresh();
  }

  internal void SnapToMaxAloowed(ITxRoboticLocationOperation location, bool shouldRefreshGraphics = true)
  {
    LocationNewStatus locationStatus = new CApMlmLocationsValidation((ITxObject) location).GetLocationStatus();
    if (location is TxWeldLocationOperation)
    {
      TxWeldLocationOperation locationOperation = location as TxWeldLocationOperation;
      if (locationStatus == 1)
      {
        TxVector txVector1 = new TxVector(locationOperation.ProjectedLocation.Translation.X, locationOperation.ProjectedLocation.Translation.Y, locationOperation.ProjectedLocation.Translation.Z);
        double pi = Math.PI;
        double num1 = -1.0 * pi;
        double permittedDeviationAngle;
        double num2 = permittedDeviationAngle = TxApplication.Options.Weld.PerpendicularPermittedDeviationAngle;
        double num3;
        double num4 = num3 = -1.0 * TxApplication.Options.Weld.PerpendicularPermittedDeviationAngle;
        double num5 = 0.0;
        double num6 = 0.0;
        double num7 = 0.0;
        double num8 = 0.0;
        double num9 = 0.0;
        double num10 = 0.0;
        TxLocationManipulationEx.GetRotationValuesFromWeldLocation(TxTransformationEx.Delta(locationOperation.ProjectedLocation, locationOperation.AbsoluteLocation), ref num5, ref num6, ref num7);
        TxLocationManipulationEx.GetRotationValuesFromWeldLocation(locationOperation.AbsoluteLocation, ref num8, ref num9, ref num10);
        num5 = pi >= num5 ? (num1 <= num5 ? num8 : num1) : pi;
        double num11 = num2 >= num6 ? (num4 <= num6 ? num9 : num4) : num2;
        num7 = permittedDeviationAngle >= num7 ? (num3 <= num7 ? num10 : num3) : permittedDeviationAngle;
        double num12 = 0.0;
        double num13 = 0.0;
        double num14 = 0.0;
        TxLocationManipulationEx.MapValuesToXYZFromWeldLocation(num5, num11, num7, ref num12, ref num13, ref num14);
        TxVector txVector2 = new TxVector(num12, num13, num14);
        TxTransformation txTransformation = new TxTransformation(txVector1, txVector2, (TxTransformation.TxRotationType) 1);
        locationOperation.AbsoluteLocation = txTransformation;
      }
    }
    else if (location is TxRoboticSeamLocationOperation)
    {
      TxRoboticSeamLocationOperation locationOperation = location as TxRoboticSeamLocationOperation;
      if (locationStatus == 1)
      {
        TxVector txVector3 = new TxVector(locationOperation.ProjectedLocation.Translation.X, locationOperation.ProjectedLocation.Translation.Y, locationOperation.AbsoluteLocation.Translation.Z);
        double num15 = 0.0;
        double num16 = 0.0;
        double num17 = 0.0;
        double num18 = 0.0;
        double num19 = 0.0;
        double num20 = 0.0;
        TxLocationManipulationEx.GetTranslationValuesFromSeamLocation(locationOperation.AbsoluteLocation, ref num15, ref num16, ref num17);
        TxLocationManipulationEx.GetTranslationValuesFromSeamLocation(locationOperation.ProjectedLocation, ref num18, ref num19, ref num20);
        double num21 = num15;
        double num22 = num19;
        double num23 = num20;
        if (num15 < num18 - TxApplication.Options.Continuous.PermittedDistance)
          num21 = num18 - TxApplication.Options.Continuous.PermittedDistance;
        else if (num15 > num18 + TxApplication.Options.Continuous.PermittedDistance)
          num21 = num18 + TxApplication.Options.Continuous.PermittedDistance;
        double num24 = 0.0;
        double num25 = 0.0;
        double num26 = 0.0;
        TxLocationManipulationEx.MapValuesToXYZFromSeamLocation(num21, num22, num23, ref num24, ref num25, ref num26);
        TxVector txVector4 = new TxVector(num24, num25, num26);
        double pi = Math.PI;
        double num27 = -1.0 * pi;
        double permittedDeviationAngle;
        double num28 = permittedDeviationAngle = TxApplication.Options.Continuous.NormalPermittedDeviationAngle;
        double num29;
        double num30 = num29 = -1.0 * TxApplication.Options.Continuous.NormalPermittedDeviationAngle;
        double num31 = 0.0;
        double num32 = 0.0;
        double num33 = 0.0;
        double num34 = 0.0;
        double num35 = 0.0;
        double num36 = 0.0;
        TxLocationManipulationEx.GetRotationValuesFromSeamLocation(TxTransformationEx.Delta(locationOperation.ProjectedLocation, locationOperation.AbsoluteLocation), ref num31, ref num32, ref num33);
        TxLocationManipulationEx.GetRotationValuesFromSeamLocation(locationOperation.AbsoluteLocation, ref num34, ref num35, ref num36);
        num31 = pi >= num31 ? (num27 <= num31 ? num34 : num27) : pi;
        double num37 = num28 >= num32 ? (num30 <= num32 ? num35 : num30) : num28;
        num33 = permittedDeviationAngle >= num33 ? (num29 <= num33 ? num36 : num29) : permittedDeviationAngle;
        double num38 = 0.0;
        double num39 = 0.0;
        double num40 = 0.0;
        TxLocationManipulationEx.MapValuesToXYZFromSeamLocation(num31, num37, num33, ref num38, ref num39, ref num40);
        TxVector txVector5 = new TxVector(num38, num39, num40);
        TxTransformation txTransformation = new TxTransformation(txVector4, txVector5, (TxTransformation.TxRotationType) 1);
        locationOperation.AbsoluteLocation = txTransformation;
      }
    }
    if (!shouldRefreshGraphics)
      return;
    this.GraphicsRefresh();
  }

  public void InitializeFollowMode() => this.InitializeFollowMode(true);

  internal void InitializeFollowMode(bool allowMountedWorkpiece)
  {
    this._followMode = new TxOlpRobotFollowMode();
    this._followMode.AllowMountedWorkpiece = allowMountedWorkpiece;
  }

  internal void FollowMode(bool followModeStatus) => this._followMode.FollowMode = followModeStatus;

  internal void RobotFollowMode(ITxLocationOperation location, bool shouldFollow)
  {
    if (shouldFollow && location != null)
    {
      this.SetGunAndRobotOnLocation(location, shouldFollow);
    }
    else
    {
      this.ResetGunAndRobotPose();
      this.DestroyGhostGun();
    }
  }

  internal bool SetGunAndRobotOnLocation(ITxLocationOperation location)
  {
    return this.SetGunAndRobotOnLocation(location, true);
  }

  internal bool SetGunAndRobotOnLocation(ITxLocationOperation location, bool shouldFollow)
  {
    bool flag = true;
    if (!shouldFollow)
      return flag;
    if (this._followMode.FollowMode && location != null && location is ITxRoboticLocationOperation)
      return this._followMode.SetGunAndRobotOnLocation(location as ITxRoboticLocationOperation);
    this.ResetGunAndRobotPose();
    this.DestroyGhostGun();
    return flag;
  }

  internal void DestroyGhostGun() => this._followMode.DestroyGhostGun();

  internal void ResetGunAndRobotPose() => this._followMode.ResetGunAndRobotPose();

  public LocationNewStatus Translate(
    double oldValueX,
    double newValueX,
    double oldValueY,
    double newValueY,
    double oldValueZ,
    double newValueZ,
    ref ITxLocationOperation theLocation,
    LocationNewStatus locationStatus,
    ref double lastLocationDelta,
    bool limitOptions,
    bool rotateToMaximum,
    bool shouldFollowModeOnTheLocation,
    TxTransformation referenceFrame)
  {
    TxTransformation txTransformation = (TxTransformation) null;
    ITxLocatableObject location = theLocation as ITxLocatableObject;
    CApMlmLocationsValidation locationsValidation = new CApMlmLocationsValidation((ITxObject) location, limitOptions, rotateToMaximum);
    LocationNewStatus locationNewStatus = (LocationNewStatus) 0;
    this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.None;
    if (location != null)
    {
      double num1 = (newValueX - oldValueX) * TxApplication.Options.Units.LinearMultiplier;
      double num2 = (newValueY - oldValueY) * TxApplication.Options.Units.LinearMultiplier;
      double num3 = (newValueZ - oldValueZ) * TxApplication.Options.Units.LinearMultiplier;
      double num4 = 0.0;
      double num5 = 0.0;
      if (!TxDoubleExtension.AlmostEquals(num1, 0.0) && TxDoubleExtension.AlmostEquals(num2, 0.0) && TxDoubleExtension.AlmostEquals(num3, 0.0))
      {
        num1 += lastLocationDelta;
        num4 = num1;
        num5 = newValueX;
      }
      else if (TxDoubleExtension.AlmostEquals(num1, 0.0) && !TxDoubleExtension.AlmostEquals(num2, 0.0) && TxDoubleExtension.AlmostEquals(num3, 0.0))
      {
        num2 += lastLocationDelta;
        num4 = num2;
        num5 = newValueY;
      }
      else if (TxDoubleExtension.AlmostEquals(num1, 0.0) && TxDoubleExtension.AlmostEquals(num2, 0.0) && !TxDoubleExtension.AlmostEquals(num3, 0.0))
      {
        num3 += lastLocationDelta;
        num4 = num3;
        num5 = newValueZ;
      }
      double deltaX = num1;
      double deltaY = num2;
      double deltaZ = num3;
      txTransformation = new TxTransformation(new TxVector(num1, num2, num3), new TxVector(), (TxTransformation.TxRotationType) 1);
      if (theLocation is TxWeldLocationOperation || theLocation is TxRoboticSeamLocationOperation)
      {
        if (theLocation is TxWeldLocationOperation)
        {
          TxWeldLocationOperation locationOperation = theLocation as TxWeldLocationOperation;
          locationNewStatus = locationsValidation.GetMaxAllowedTranslationDelta(referenceFrame, ref deltaX, ref deltaY, ref deltaZ);
        }
        else if (theLocation is TxRoboticSeamLocationOperation)
        {
          TxRoboticSeamLocationOperation locationOperation = theLocation as TxRoboticSeamLocationOperation;
          locationNewStatus = locationsValidation.GetMaxAllowedTranslationDelta(referenceFrame, ref deltaX, ref deltaY, ref deltaZ);
        }
        TxTransformation deltaTransformation1 = new TxTransformation(new TxVector(deltaX, deltaY, deltaZ), new TxVector(0.0, 0.0, 0.0), (TxTransformation.TxRotationType) 1);
        double num6 = 0.0;
        double num7 = 0.0;
        double num8 = 0.0;
        double num9 = 0.0;
        if (!TxDoubleExtension.AlmostEquals(num1, 0.0) && TxDoubleExtension.AlmostEquals(num2, 0.0) && TxDoubleExtension.AlmostEquals(num3, 0.0))
        {
          num6 = deltaX;
          num7 = -1.0 * (oldValueX * TxApplication.Options.Units.LinearMultiplier);
        }
        else if (TxDoubleExtension.AlmostEquals(num1, 0.0) && !TxDoubleExtension.AlmostEquals(num2, 0.0) && TxDoubleExtension.AlmostEquals(num3, 0.0))
        {
          num6 = deltaY;
          num8 = -1.0 * (oldValueY * TxApplication.Options.Units.LinearMultiplier);
        }
        else if (TxDoubleExtension.AlmostEquals(num1, 0.0) && TxDoubleExtension.AlmostEquals(num2, 0.0) && !TxDoubleExtension.AlmostEquals(num3, 0.0))
        {
          num6 = deltaZ;
          num9 = -1.0 * (oldValueZ * TxApplication.Options.Units.LinearMultiplier);
        }
        else
        {
          num7 = -1.0 * (oldValueX * TxApplication.Options.Units.LinearMultiplier);
          num8 = -1.0 * (oldValueY * TxApplication.Options.Units.LinearMultiplier);
          num9 = -1.0 * (oldValueZ * TxApplication.Options.Units.LinearMultiplier);
        }
        switch ((int) locationNewStatus)
        {
          case 0:
            TxTransformation newTransformation1 = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation1, referenceFrame);
            TxTransformation absoluteLocation1 = location.AbsoluteLocation;
            location.AbsoluteLocation = newTransformation1;
            if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
            {
              location.AbsoluteLocation = absoluteLocation1;
              locationNewStatus = (LocationNewStatus) 1;
              this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
              break;
            }
            lastLocationDelta = 0.0;
            this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
            break;
          case 1:
            bool flag = true;
            if (locationStatus == null || locationStatus == 2)
            {
              TxTransformation deltaTransformation2 = new TxTransformation(new TxVector(num7, num8, num9), new TxVector(0.0, 0.0, 0.0), (TxTransformation.TxRotationType) 1);
              TxTransformation newTransformation2 = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation2, referenceFrame);
              TxTransformation absoluteLocation2 = location.AbsoluteLocation;
              location.AbsoluteLocation = newTransformation2;
              if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
              {
                location.AbsoluteLocation = absoluteLocation2;
                flag = false;
                this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
              }
              else
                this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
            }
            if (flag)
            {
              lastLocationDelta = num5 * TxApplication.Options.Units.LinearMultiplier;
              break;
            }
            break;
          case 2:
            TxTransformation newTransformation3 = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation1, referenceFrame);
            TxTransformation absoluteLocation3 = location.AbsoluteLocation;
            location.AbsoluteLocation = newTransformation3;
            if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
            {
              location.AbsoluteLocation = absoluteLocation3;
              locationNewStatus = (LocationNewStatus) 1;
              this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
              break;
            }
            lastLocationDelta = num4 - num6;
            this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
            break;
        }
        LocationNewStatus locationStatus1 = locationsValidation.GetLocationStatus();
        locationNewStatus = TxLocationManipulationEx.GetBestLocationNewStatus(locationNewStatus, locationStatus1);
      }
      else if (theLocation is TxRoboticViaLocationOperation || theLocation is TxGenericRoboticLocationOperation || theLocation is TxObjectFlowLocationOperation)
      {
        locationNewStatus = (LocationNewStatus) 0;
        TxTransformation deltaTransformation = new TxTransformation(new TxVector(num1, num2, num3), new TxVector(0.0, 0.0, 0.0), (TxTransformation.TxRotationType) 1);
        TxTransformation newTransformation = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation, referenceFrame);
        TxTransformation absoluteLocation = location.AbsoluteLocation;
        location.AbsoluteLocation = newTransformation;
        if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
        {
          location.AbsoluteLocation = absoluteLocation;
          locationNewStatus = (LocationNewStatus) 1;
          this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
        }
        else
        {
          lastLocationDelta = 0.0;
          this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
        }
      }
    }
    return locationNewStatus;
  }

  public LocationNewStatus Rotate(
    double oldValue,
    double newValue,
    TxAxis rotatingAxis,
    ref ITxLocationOperation theLocation,
    LocationNewStatus locationStatus,
    ref double lastLocationDelta,
    bool limitOptions,
    bool rotateToMaximum,
    bool shouldFollowModeOnTheLocation,
    TxTransformation relativeFrame)
  {
    TxTransformation txTransformation = (TxTransformation) null;
    ITxLocatableObject location = theLocation as ITxLocatableObject;
    CApMlmLocationsValidation locationsValidation = new CApMlmLocationsValidation((ITxObject) location, limitOptions, rotateToMaximum);
    LocationNewStatus locationNewStatus = (LocationNewStatus) 0;
    this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.None;
    if (location != null)
    {
      double num1 = (newValue - oldValue) * TxApplication.Options.Units.AngularMultiplier + lastLocationDelta;
      double num2 = 0.0;
      double num3 = 0.0;
      double num4 = 0.0;
      if (rotatingAxis == null)
        num2 = num1;
      else if (rotatingAxis == 1)
        num3 = num1;
      else
        num4 = num1;
      double deltaX = num2;
      double deltaY = num3;
      double deltaZ = num4;
      txTransformation = new TxTransformation(new TxVector(), new TxVector(num2, num3, num4), (TxTransformation.TxRotationType) 1);
      if (theLocation is TxWeldLocationOperation || theLocation is TxRoboticSeamLocationOperation)
      {
        if (theLocation is TxWeldLocationOperation)
        {
          TxWeldLocationOperation locationOperation = theLocation as TxWeldLocationOperation;
          locationNewStatus = locationsValidation.GetMaxAllowedRotationDelta(ref deltaX, ref deltaY, ref deltaZ);
        }
        else if (theLocation is TxRoboticSeamLocationOperation)
        {
          TxRoboticSeamLocationOperation locationOperation = theLocation as TxRoboticSeamLocationOperation;
          locationNewStatus = locationsValidation.GetMaxAllowedRotationDelta(ref deltaX, ref deltaY, ref deltaZ);
        }
        double num5 = 0.0;
        double num6 = 0.0;
        double num7 = 0.0;
        double num8 = 0.0;
        if (rotatingAxis == null)
        {
          num5 = deltaX;
          num6 = -1.0 * (oldValue * TxApplication.Options.Units.AngularMultiplier);
          deltaY = deltaZ = 0.0;
        }
        else if (rotatingAxis == 1)
        {
          num5 = deltaY;
          num7 = -1.0 * (oldValue * TxApplication.Options.Units.AngularMultiplier);
          deltaX = deltaZ = 0.0;
        }
        else if (rotatingAxis == 2)
        {
          num5 = deltaZ;
          num8 = -1.0 * (oldValue * TxApplication.Options.Units.AngularMultiplier);
          deltaX = deltaY = 0.0;
        }
        TxTransformation deltaTransformation1 = new TxTransformation(new TxVector(0.0, 0.0, 0.0), new TxVector(deltaX, deltaY, deltaZ), (TxTransformation.TxRotationType) 1);
        switch ((int) locationNewStatus)
        {
          case 0:
            TxTransformation newTransformation1 = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation1, relativeFrame);
            TxTransformation absoluteLocation1 = location.AbsoluteLocation;
            location.AbsoluteLocation = newTransformation1;
            if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
            {
              location.AbsoluteLocation = absoluteLocation1;
              this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
              break;
            }
            lastLocationDelta = 0.0;
            this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
            break;
          case 1:
            bool flag = true;
            if (locationStatus == null || locationStatus == 2)
            {
              TxTransformation deltaTransformation2 = new TxTransformation(new TxVector(0.0, 0.0, 0.0), new TxVector(num6, num7, num8), (TxTransformation.TxRotationType) 1);
              TxTransformation newTransformation2 = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation2, relativeFrame);
              TxTransformation absoluteLocation2 = location.AbsoluteLocation;
              location.AbsoluteLocation = newTransformation2;
              if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
              {
                location.AbsoluteLocation = absoluteLocation2;
                flag = false;
                this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
              }
              else
                this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
            }
            if (flag)
            {
              lastLocationDelta = newValue * TxApplication.Options.Units.AngularMultiplier;
              break;
            }
            break;
          case 2:
            TxTransformation newTransformation3 = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation1, relativeFrame);
            TxTransformation absoluteLocation3 = location.AbsoluteLocation;
            location.AbsoluteLocation = newTransformation3;
            if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
            {
              location.AbsoluteLocation = absoluteLocation3;
              this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
              break;
            }
            lastLocationDelta = num1 - num5;
            this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
            break;
        }
        LocationNewStatus locationStatus1 = locationsValidation.GetLocationStatus();
        locationNewStatus = TxLocationManipulationEx.GetBestLocationNewStatus(locationNewStatus, locationStatus1);
      }
      else if (theLocation is TxRoboticViaLocationOperation || theLocation is TxGenericRoboticLocationOperation || theLocation is TxObjectFlowLocationOperation)
      {
        locationNewStatus = (LocationNewStatus) 0;
        TxTransformation deltaTransformation = new TxTransformation(new TxVector(0.0, 0.0, 0.0), new TxVector(num2, num3, num4), (TxTransformation.TxRotationType) 1);
        TxTransformation newTransformation = CApMlmSharedLogic.CalculateLocationNewTransformation(location, deltaTransformation, relativeFrame);
        TxTransformation absoluteLocation = location.AbsoluteLocation;
        location.AbsoluteLocation = newTransformation;
        if (!this.SetGunAndRobotOnLocation((ITxLocationOperation) (theLocation as ITxRoboticLocationOperation), shouldFollowModeOnTheLocation))
        {
          location.AbsoluteLocation = absoluteLocation;
          this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Failure;
        }
        else
        {
          lastLocationDelta = 0.0;
          this.LastGunAndRobotOnLocationState = CApMlmSharedLogic.eGunAndRobotOnLocationState.Success;
        }
      }
    }
    return locationNewStatus;
  }

  internal void GenerateConeManipulator()
  {
    if (this.ShowConeManipulator && this.Location != null && this.Location is ITxRoboticLocationOperation)
      this.CreateConeManipulator(this.Location as ITxRoboticLocationOperation);
    else
      this.DestroyManipulator();
  }

  internal bool CanHaveProjection()
  {
    return this.Location is TxWeldLocationOperation || this.Location is TxRoboticSeamLocationOperation;
  }

  private bool isGreaterOrEqual(double val1, double val2)
  {
    return TxDoubleExtension.AlmostEquals(val1, val2) || TxDoubleExtension.IsGreaterThan(val1, val2, 1E-07);
  }

  private bool isLessOrEqual(double val1, double val2)
  {
    return TxDoubleExtension.AlmostEquals(val1, val2) || val1 - val2 < 1E-07;
  }

  private bool isDevAngleInValidRange(double devAngle)
  {
    double val2_1 = 0.01;
    double val2_2 = 89.0;
    double val1 = devAngle * (180.0 / Math.PI);
    return this.isGreaterOrEqual(val1, val2_1) && this.isLessOrEqual(val1, val2_2);
  }

  private double getConeHeightForRestrictedRadius(double devAngle)
  {
    double num1 = 50.0;
    double num2 = devAngle * (180.0 / Math.PI);
    double restrictedRadius;
    if (num2 > num1)
    {
      restrictedRadius = 238.351 / Math.Tan(devAngle);
    }
    else
    {
      double num3 = 4.0;
      restrictedRadius = num2 * num3;
    }
    return restrictedRadius;
  }

  internal bool CanCreateConeManipulator()
  {
    if (this.Location is TxWeldLocationOperation)
    {
      if (this.isDevAngleInValidRange(TxApplication.Options.Weld.PerpendicularPermittedDeviationAngle))
        return true;
    }
    else if (this.Location is TxRoboticSeamLocationOperation && this.isDevAngleInValidRange(TxApplication.Options.Continuous.NormalPermittedDeviationAngle))
      return true;
    return false;
  }

  private void CreateConeManipulator(ITxRoboticLocationOperation location)
  {
    this.DestroyManipulator();
    bool flag = false;
    double num1 = 0.0;
    double num2 = 0.0;
    double num3 = 0.0;
    double num4 = 0.0;
    double num5 = 0.0;
    TxTransformation txTransformation1 = new TxTransformation();
    if (location is TxWeldLocationOperation)
    {
      num2 = TxApplication.Options.Weld.PerpendicularPermittedDeviationAngle;
      if (this.isDevAngleInValidRange(num2))
      {
        num1 = this.getConeHeightForRestrictedRadius(num2);
        txTransformation1 = new TxTransformation((location as TxWeldLocationOperation).AbsoluteLocation.Translation, (location as TxWeldLocationOperation).ProjectedLocation.RotationRPY_XYZ, (TxTransformation.TxRotationType) 1);
        TxLocationManipulationEx.MapValuesToXYZFromWeldLocation(num1, 0.0, 0.0, ref num3, ref num4, ref num5);
        flag = true;
      }
    }
    else if (location is TxRoboticSeamLocationOperation)
    {
      num2 = TxApplication.Options.Continuous.NormalPermittedDeviationAngle;
      if (this.isDevAngleInValidRange(num2))
      {
        num1 = this.getConeHeightForRestrictedRadius(num2);
        txTransformation1 = new TxTransformation((location as TxRoboticSeamLocationOperation).AbsoluteLocation.Translation, (location as TxRoboticSeamLocationOperation).ProjectedLocation.RotationRPY_XYZ, (TxTransformation.TxRotationType) 1);
        TxLocationManipulationEx.MapValuesToXYZFromSeamLocation(num1, 0.0, 0.0, ref num3, ref num4, ref num5);
        flag = true;
      }
    }
    if (!flag)
      return;
    TxTransformation txTransformation2 = new TxTransformation(new TxVector(num3, num4, num5), new TxVector(), (TxTransformation.TxRotationType) 1);
    TxManipulatorConeElementData manipulatorConeElementData = new TxManipulatorConeElementData(new TxTransformation(new TxVector(0.0, 0.0, 0.0), new TxVector(0.0, 0.0, 0.0), (TxTransformation.TxRotationType) 1), TxTransformation.op_Multiply(txTransformation1, txTransformation2).Translation, txTransformation1.Translation, num1 * Math.Tan(num2));
    TxColor txColorYellow = TxColor.TxColorYellow;
    txColorYellow.Transparency = 0.5;
    ((TxManipulatorElementData) manipulatorConeElementData).Color = txColorYellow;
    ((TxManipulatorElementData) manipulatorConeElementData).Pickable = false;
    ((TxManipulatorElementData) manipulatorConeElementData).AlwaysOnTop = true;
    ((TxManipulatorElementData) manipulatorConeElementData).Unzoomable = false;
    ((TxManipulatorElementData) manipulatorConeElementData).Visibility = (TxManipulatorElementData.TxManipulatorElementVisibility) 49344;
    TxManipulatorCreationData manipulatorCreationData = new TxManipulatorCreationData();
    ((TxLocatableObjectCreationData) manipulatorCreationData).AbsoluteLocation = new TxTransformation();
    try
    {
      this._coneManipulator = TxApplication.ActiveDocument.PhysicalRoot.CreateManipulator(manipulatorCreationData);
      this._coneManipulator.AddElement((TxManipulatorElementData) manipulatorConeElementData);
      this._coneManipulator.Display();
    }
    catch
    {
    }
  }

  private void DestroyManipulator()
  {
    if (!TxManipulator.op_Inequality(this._coneManipulator, (ITxObject) null))
      return;
    this._coneManipulator.RemoveAllElements();
    TxObjectEx.ForceDeletion((ITxObject) this._coneManipulator);
    this._coneManipulator = (TxManipulator) null;
  }

  internal void GraphicsRefresh(ITxLocationOperation location = null)
  {
    this.GenerateConeManipulator();
    TxApplication.RefreshDisplay();
  }

  internal void ZoomToSelection()
  {
    TxApplication.CommandsManager.ExecuteCommand("GraphicViewer.ZoomToSelection");
    TxApplication.RefreshDisplay();
  }

  internal static void SetActiveControlState(CheckBox c)
  {
  }

  internal static bool GetBoolValueFromRegistry(Form form, string vlaueName)
  {
    bool valueFromRegistry = false;
    object obj = (object) null;
    TxFormSettings.RestoreFormValue(form, vlaueName, ref obj);
    if (obj != null)
    {
      try
      {
        valueFromRegistry = bool.Parse(obj as string);
      }
      catch (Exception ex)
      {
        valueFromRegistry = false;
      }
    }
    return valueFromRegistry;
  }

  private static TxTransformation CalculateLocationNewTransformation(
    ITxLocatableObject obj,
    TxTransformation deltaTransformation,
    TxTransformation relativeFrame)
  {
    TxTransformation txTransformation1 = new TxTransformation(obj.AbsoluteLocation.Translation, obj.AbsoluteLocation.RotationRPY_XYZ, (TxTransformation.TxRotationType) 1);
    TxTransformation newTransformation = TxTransformation.op_Multiply(txTransformation1, deltaTransformation);
    if (TxTransformation.op_Inequality(relativeFrame, (TxTransformation) null))
    {
      TxTransformation txTransformation2 = new TxTransformation();
      deltaTransformation.Transpose(txTransformation2);
      TxTransformation txTransformation3 = new TxTransformation();
      relativeFrame.Transpose(txTransformation3);
      TxTransformation txTransformation4 = new TxTransformation();
      txTransformation1.Transpose(txTransformation4);
      TxTransformation txTransformation5 = TxTransformation.op_Multiply(txTransformation4, txTransformation3.Inverse);
      deltaTransformation = TxTransformation.op_Multiply(txTransformation5, TxTransformation.op_Multiply(txTransformation2, txTransformation5.Inverse));
      TxTransformation.op_Multiply(deltaTransformation, txTransformation4).Transpose(newTransformation);
    }
    return newTransformation;
  }

  private void ClearFrame(ref TxFrame f)
  {
    if (!TxFrame.op_Inequality(f, (ITxObject) null))
      return;
    f.Delete();
    f = (TxFrame) null;
  }

  public void ClearBoldFrame() => this.ClearFrame(ref this._tmpFrame);

  public void BlankBoldFrame()
  {
    if (!TxFrame.op_Inequality(this._tmpFrame, (ITxObject) null))
      return;
    this._tmpFrame.Blank();
    TxApplication.RefreshDisplay();
  }

  public void DisplayBoldFrame()
  {
    if (!TxFrame.op_Inequality(this._tmpFrame, (ITxObject) null))
      return;
    this._tmpFrame.Display();
    TxApplication.RefreshDisplay();
  }

  public void CreateBoldFrame(TxTransformation location)
  {
    if (!TxTransformation.op_Inequality(location, (TxTransformation) null) || location.Equals((object) TxApplication.ActiveDocument.WorkingFrame))
      return;
    this._tmpFrame = this.CreateFrame("", location);
  }

  public void ClearProjectionFrame() => this.ClearFrame(ref this._tmpProjectionFrame);

  public void BlankProjectionFrame()
  {
    if (!TxFrame.op_Inequality(this._tmpProjectionFrame, (ITxObject) null) || this._tmpProjectionFrame.Visibility == 2)
      return;
    this._tmpProjectionFrame.Blank();
    TxApplication.RefreshDisplay();
  }

  public void DisplayProjectionFrame()
  {
    if (!TxFrame.op_Inequality(this._tmpProjectionFrame, (ITxObject) null))
      return;
    this._tmpProjectionFrame.Display();
    TxApplication.RefreshDisplay();
  }

  public void CreateProjectionFrame(TxTransformation projectedLocation)
  {
    if (!TxTransformation.op_Inequality(projectedLocation, (TxTransformation) null))
      return;
    this._tmpProjectionFrame = this.CreateFrame("Projection Frame", projectedLocation);
  }

  public void UpdateProjectionFrame(bool canCreateFrame, TxTransformation projectedLocation)
  {
    if (canCreateFrame)
    {
      this.ClearProjectionFrame();
      this.CreateProjectionFrame(projectedLocation);
      this.DisplayProjectionFrame();
    }
    else
      this.BlankProjectionFrame();
  }

  private TxFrame CreateFrame(string name, TxTransformation location)
  {
    TxFrame frame = TxFrameEx.CreateFrame(new TxFrameCreationDataEx(name, location, true));
    frame.Color = TxColor.TxColorDarkGreen;
    return frame;
  }

  public void FillFrameComboBox(TxFrameComboBoxCtrl comboBoxCtrl)
  {
    this._workingFrame = this.CreateFrame(MlmStringTable.ResourceManager.GetString("MLM_WORKING_FRAME"), new TxTransformation(TxApplication.ActiveDocument.WorkingFrame));
    this._workingFrame.Blank();
    comboBoxCtrl.AddFrame(this._workingFrame);
    comboBoxCtrl.SelectFrame(MlmStringTable.ResourceManager.GetString("MLM_WORKING_FRAME"));
  }

  public TxFrame GetPlacementManipulatorFrameWithLocationSet(TxTransformation location)
  {
    if (TxFrame.op_Equality(this._placementManipulatorFrame, (ITxObject) null))
    {
      this._placementManipulatorFrame = this.CreateFrame("", new TxTransformation());
      this._placementManipulatorFrame.Blank();
    }
    this._placementManipulatorFrame.AbsoluteLocation = location;
    return this._placementManipulatorFrame;
  }

  public void Dispose()
  {
    this.ClearBoldFrame();
    this.ClearProjectionFrame();
    this.ClearFrame(ref this._workingFrame);
    this.ClearFrame(ref this._placementManipulatorFrame);
  }

  public string ProjectionFrameDisplayName
  {
    get
    {
      return !TxFrame.op_Inequality(this._tmpProjectionFrame, (ITxObject) null) ? (string) null : this._tmpProjectionFrame.Name;
    }
  }

  public TxTransformation ProjectionFrameLocation
  {
    get
    {
      return !TxFrame.op_Inequality(this._tmpProjectionFrame, (ITxObject) null) ? (TxTransformation) null : this._tmpProjectionFrame.AbsoluteLocation;
    }
  }

  public enum eGunAndRobotOnLocationState
  {
    None,
    Success,
    Failure,
  }
}
